package com.huifaapp.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.huifaapp.mall.R;
import com.huifaapp.mall.bean.JDcomjPlistBean;
import com.huifaapp.mall.bean.Jdred_packetlistBean;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by duanyu on 2018/1/30.
 */

public class JDred_packetAdapter extends  XRecyclerView.Adapter<JDred_packetAdapter.MyViewHolder> implements View.OnClickListener{
    private List<Jdred_packetlistBean.DataBean.ListBean>list;
    private Context context;
    private List<Boolean> isChecked;    //每个条目是否为选中
    private List<String> numbers;       //每个条目应显示的数字
    private List<Integer> checkedIndex;   //保存选中的下标
    private List<CheckBox> checkBox;      //保存选中的checkBox
    private int maxChecked = 9;       //最多选中几个
    private int checkedNumber = 0;    //选中的个数
    private int length = 50;  //条目的总长
    public JDred_packetAdapter(List<Jdred_packetlistBean.DataBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
            isChecked = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                isChecked.add(false);
            }
            numbers = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                numbers.add("");
            }
            checkedIndex = new ArrayList<>();
            checkBox = new ArrayList<>();
        }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View recyclerViewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.itme_tmall_choiceness, parent, false);
        recyclerViewItem.setOnClickListener(this);
        return new MyViewHolder(recyclerViewItem);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.itme_tmall_text_name.setText("\t\t\t\t"+list.get(position).getGoodsName());
        Glide.with(context).load(list.get(position).getMainImageUrl()).into(holder.itme_tmall_imge);
        holder.itme_tmall_price.setText(list.get(position).getPrice()+"");
        holder. itme_tmall_discountprice.setText(list.get(position).getConPrice()+"");
        holder.itme_tmall_totalSales.setText(list.get(position).getTotalSales()+"");
        holder.itme_tmall_couponMoney.setText("红包劵"+list.get(position).getCouponMoney());
        holder.itme_tmall_checkBox.setText(numbers.get(position));
        holder.itme_tmall_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean check;
                if (checkedNumber < maxChecked) {
                    if (!isChecked.get(position)) {
                        checkedIndex.add(position);
                        checkBox.add(holder.itme_tmall_checkBox);
                        checkedNumber++;
                        setText();
                    } else {
                        clearText(position, holder.itme_tmall_checkBox);
                    }
                    check = !isChecked.get(position);
                } else {
                    if (checkedNumber == maxChecked && isChecked.get(position)) {
                        clearText(position, holder.itme_tmall_checkBox);
                    } else {
                        Toast.makeText(context, "最多只能选" + maxChecked + "个", Toast.LENGTH_SHORT).show();
                    }
                    check = false;
                }
                isChecked.set(position, check);
                holder.itme_tmall_checkBox.setChecked(isChecked.get(position));
            }
        });
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (mMyItemclickListener != null) {
            mMyItemclickListener.itemclick(v, (Integer) v.getTag());
        }
    }
    public void clearText(int position, CheckBox cb_checked) {
        for (int i = 0; i < checkedIndex.size(); i++) {
            if (checkedIndex.get(i) == position) {
                numbers.set(position, "");
                checkedIndex.remove(i);
                checkBox.remove(i);
                break;
            }
        }
        checkedNumber--;
        setText();
        numbers.set(position, "");
        cb_checked.setText("");
    }

    public void setText() {
        for (int i = 0; i < checkedIndex.size(); i++) {
            numbers.set(checkedIndex.get(i), (i + 1) + "");
            checkBox.get(i).setText(numbers.get(checkedIndex.get(i)));
        }
    }
    public interface MyItemclickListener {
        void itemclick(View view, int position);
    }
    public MyItemclickListener mMyItemclickListener;

    public void setmMyItemclickListener(MyItemclickListener mMyItemclickListener) {
        this.mMyItemclickListener = mMyItemclickListener;
    }
    class MyViewHolder extends RecyclerView.ViewHolder {

        private final ImageView itme_tmall_imge;
        private final TextView itme_tmall_text_name;
        private final TextView itme_tmall_price;
        private final TextView itme_tmall_discountprice;
        private final CheckBox itme_tmall_checkBox;
        private final TextView itme_tmall_totalSales;
        private final TextView itme_tmall_couponMoney;

        public MyViewHolder(View itemView) {
            super(itemView);
            itme_tmall_imge = (ImageView) itemView.findViewById(R.id.itme_tmall_imge);
            itme_tmall_text_name = (TextView) itemView.findViewById(R.id.itme_tmall_text_name);
            itme_tmall_price = (TextView) itemView.findViewById(R.id.itme_tmall_price);
            itme_tmall_discountprice = (TextView) itemView.findViewById(R.id.itme_tmall_discountprice);
            itme_tmall_checkBox = (CheckBox) itemView.findViewById(R.id.itme_tmall_checkBox);
            itme_tmall_totalSales = (TextView) itemView.findViewById(R.id.itme_tmall_totalSales);
            itme_tmall_couponMoney = (TextView) itemView.findViewById(R.id.itme_tmall_couponMoney);
        }
    }
}
